require "test/unit"
class Test_Class<Test::Unit::TestCase
  def test_creating_hases
    empty_hash = Hash.new
    assert_equal Hash, empty_hash.class
    assert_equal({}, empty_hash)
    assert_equal 0, empty_hash.size
  end

  def test_hash_literals
    hash = { :one => "uno", :two => "dos"}
    assert_equal 2, hash.size
  end   
  # Hash中的key和value是成对出现的，键对值，一个key对应一个value

  def test_accessing_hashes
    hash = { :one => "uno", :two => "dos"}
    assert_equal "uno", hash[:one]
    assert_equal "dos", hash[:two]
    assert_equal nil,   hash[:doesnt_exist]
  end  
   #取对应值

  def test_changing_hashes
    hash = {:one => "uno", :two => "dos"}
    hash[:one] = "eins"
    expected = {:one => "eins", :two => "dos"}
    assert_equal true, expected == hash
   #这里将:one的值修改为eins， 同时改变hash里原有的值
  end

  def test_hash_is_unordered
    hash1 = {:one => "uno", :two => "dos"}
    hash2 = {:two => "dos", :one => "uno"}
    assert_equal true, hash1 == hash2 
  end
  #  hash不像array那样会讲究元素顺序，只要里面的键对值相同那么这两个hash相同

  def test_hash_keys
    hash = {:one => "uno", :two => "dos"}
    assert_equal 2,      hash.keys.size
    assert_equal true,   hash.keys.include?(:one)
    assert_equal true,   hash.keys.include?(:two)
    assert_equal Array,  hash.keys.class
     
    assert_equal 2,      hash.values.size
    assert_equal true,   hash.values.include?("uno")
    assert_equal true,   hash.values.include?("dos")
    assert_equal Array,  hash.values.class
  end  
  #显示keys和values的数量，  hash中的keys和values都属于Array的类

  def test_combinbing_hashes
    hash = {"jim" => 53, "amy" => 20, "dan" => 23} 
    new_hash = hash.merge({"jim" => 54, "jenny" => 26})

    assert_equal true, hash != new_hash
    expected = {"jim" => 54, "amy" => 20, "dan" => 23, "jenny" => 26}
    assert_equal true, expected == new_hash
  end
  # hash.merge的用法，添加键对值到，hash里面去，如果里面有相同的key的话那么修改原有的values,形成新的hash
end
